package com.itsu.vtalk.exception.handler;

import com.alibaba.fastjson.JSON;
import com.itsu.vtalk.exception.LoginAuthException;
import com.itsu.vtalk.exception.SystemBaseException;
import com.itsu.vtalk.util.ServletUtil;
import com.itsu.vtalk.vo.RestEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author suben
 * @createtime 2020/6/21 10:46
 **/
@ControllerAdvice
public class ErrorHandle {

    private static Logger logger = LoggerFactory.getLogger(ErrorHandle.class);

    @ExceptionHandler(value = Throwable.class)
    public Object handleBase(HttpServletRequest request, HttpServletResponse response, Throwable throwable) {
        logger.error("Not expect error happen", throwable);
        if (ServletUtil.isAjax(request)) {
            try (PrintWriter pw = response.getWriter()) {
                pw.write(JSON.toJSONString(RestEntity.UNEXPECTED_ERROR));
                pw.flush();
            } catch (IOException e) {
                logger.error("write error result error", e);
            }
            return null;
        } else {
            ModelAndView mav = new ModelAndView("/error/500");
            mav.addAllObjects(RestEntity.UNEXPECTED_ERROR.tansfor2Map());
            return mav;
        }
    }

    @ExceptionHandler(value = SystemBaseException.class)
    @ResponseStatus(HttpStatus.OK)
    public Object handleSystemBase(HttpServletRequest request, HttpServletResponse response, Throwable throwable) {
        logger.error("SystemBase error happen", throwable);
        SystemBaseException error = (SystemBaseException) throwable;
        RestEntity restEntity = RestEntity.build()
                .withReturnCode(error.getErrorCode())
                .withReturnMsg(error.getErrorMsg())
                .create();

        if (ServletUtil.isAjax(request)) {
            try (PrintWriter pw = response.getWriter()) {
                pw.write(JSON.toJSONString(restEntity));
                pw.flush();
            } catch (IOException e) {
                logger.error("write error result error", e);
            }
            return null;
        } else {
            ModelAndView mav = new ModelAndView("redirect:/error/500");
            mav.addAllObjects(restEntity.tansfor2Map());
            return mav;
        }
    }

    @ExceptionHandler(value = LoginAuthException.class)
    public Object handleLoginAuth(HttpServletRequest request, HttpServletResponse response, Throwable throwable) {
        logger.error("LoginAuth error happen", throwable);
        LoginAuthException error = (LoginAuthException) throwable;
        RestEntity restEntity = RestEntity.build()
                .withReturnCode(error.getErrorCode())
                .withReturnMsg(error.getErrorMsg())
                .create();

        if (ServletUtil.isAjax(request)) {
            try (PrintWriter pw = response.getWriter()) {
                pw.write(JSON.toJSONString(restEntity));
                pw.flush();
            } catch (IOException e) {
                logger.error("write error result error", e);
            }
            return null;
        } else {
            ModelAndView mav = new ModelAndView("/login");
            mav.addAllObjects(restEntity.tansfor2Map());
            return mav;
        }
    }
}
